using System.ComponentModel.DataAnnotations;

namespace DocumentCreationSystem.Models.AgentOps
{
    /// <summary>
    /// Agent工作流模型
    /// </summary>
    public class AgentWorkflow
    {
        [Key]
        public string Id { get; set; } = Guid.NewGuid().ToString();

        /// <summary>
        /// 工作流名称
        /// </summary>
        public string Name { get; set; } = string.Empty;

        /// <summary>
        /// 工作流描述
        /// </summary>
        public string Description { get; set; } = string.Empty;

        /// <summary>
        /// 工作流版本
        /// </summary>
        public string Version { get; set; } = "1.0.0";

        /// <summary>
        /// 创建时间
        /// </summary>
        public DateTime CreatedAt { get; set; } = DateTime.Now;

        /// <summary>
        /// 更新时间
        /// </summary>
        public DateTime UpdatedAt { get; set; } = DateTime.Now;

        /// <summary>
        /// 工作流状态
        /// </summary>
        public WorkflowStatus Status { get; set; } = WorkflowStatus.Created;

        /// <summary>
        /// 工作流类型
        /// </summary>
        public WorkflowType Type { get; set; }

        /// <summary>
        /// 优先级
        /// </summary>
        public WorkflowPriority Priority { get; set; } = WorkflowPriority.Normal;

        /// <summary>
        /// 工作流步骤
        /// </summary>
        public List<WorkflowStep> Steps { get; set; } = new();

        /// <summary>
        /// 工作流配置
        /// </summary>
        public WorkflowConfiguration Configuration { get; set; } = new();

        /// <summary>
        /// 执行上下文
        /// </summary>
        public Dictionary<string, object> Context { get; set; } = new();

        /// <summary>
        /// 工作流标签
        /// </summary>
        public List<string> Tags { get; set; } = new();

        /// <summary>
        /// 关联的项目ID
        /// </summary>
        public string? ProjectId { get; set; }

        /// <summary>
        /// 创建者ID
        /// </summary>
        public string? CreatedBy { get; set; }

        /// <summary>
        /// 源规划模板ID
        /// </summary>
        public string? SourceTemplateId { get; set; }
    }

    /// <summary>
    /// 工作流状态枚举
    /// </summary>
    public enum WorkflowStatus
    {
        /// <summary>
        /// 已创建
        /// </summary>
        Created,

        /// <summary>
        /// 等待执行
        /// </summary>
        Pending,

        /// <summary>
        /// 执行中
        /// </summary>
        Running,

        /// <summary>
        /// 已暂停
        /// </summary>
        Paused,

        /// <summary>
        /// 已完成
        /// </summary>
        Completed,

        /// <summary>
        /// 已失败
        /// </summary>
        Failed,

        /// <summary>
        /// 已取消
        /// </summary>
        Cancelled,

        /// <summary>
        /// 已超时
        /// </summary>
        Timeout
    }

    /// <summary>
    /// 工作流类型枚举
    /// </summary>
    public enum WorkflowType
    {
        /// <summary>
        /// 角色规划工作流
        /// </summary>
        CharacterPlanning,

        /// <summary>
        /// 内容创作工作流
        /// </summary>
        ContentCreation,

        /// <summary>
        /// 质量控制工作流
        /// </summary>
        QualityControl,

        /// <summary>
        /// 项目管理工作流
        /// </summary>
        ProjectManagement,

        /// <summary>
        /// 自定义工作流
        /// </summary>
        Custom
    }

    /// <summary>
    /// 工作流优先级枚举
    /// </summary>
    public enum WorkflowPriority
    {
        /// <summary>
        /// 低优先级
        /// </summary>
        Low = 1,

        /// <summary>
        /// 普通优先级
        /// </summary>
        Normal = 2,

        /// <summary>
        /// 高优先级
        /// </summary>
        High = 3,

        /// <summary>
        /// 紧急优先级
        /// </summary>
        Critical = 4
    }

    /// <summary>
    /// 工作流步骤
    /// </summary>
    public class WorkflowStep
    {
        /// <summary>
        /// 步骤ID
        /// </summary>
        public string Id { get; set; } = Guid.NewGuid().ToString();

        /// <summary>
        /// 步骤名称
        /// </summary>
        public string Name { get; set; } = string.Empty;

        /// <summary>
        /// 步骤描述
        /// </summary>
        public string Description { get; set; } = string.Empty;

        /// <summary>
        /// 步骤类型
        /// </summary>
        public StepType Type { get; set; }

        /// <summary>
        /// 执行顺序
        /// </summary>
        public int Order { get; set; }

        /// <summary>
        /// 步骤状态
        /// </summary>
        public StepStatus Status { get; set; } = StepStatus.Pending;

        /// <summary>
        /// 依赖的步骤ID列表
        /// </summary>
        public List<string> Dependencies { get; set; } = new();

        /// <summary>
        /// 步骤配置
        /// </summary>
        public Dictionary<string, object> Configuration { get; set; } = new();

        /// <summary>
        /// 预估执行时间（分钟）
        /// </summary>
        public int EstimatedDurationMinutes { get; set; }

        /// <summary>
        /// 实际执行时间（分钟）
        /// </summary>
        public int? ActualDurationMinutes { get; set; }

        /// <summary>
        /// 开始时间
        /// </summary>
        public DateTime? StartTime { get; set; }

        /// <summary>
        /// 完成时间
        /// </summary>
        public DateTime? CompletedTime { get; set; }

        /// <summary>
        /// 执行结果
        /// </summary>
        public string? Result { get; set; }

        /// <summary>
        /// 错误信息
        /// </summary>
        public string? ErrorMessage { get; set; }

        /// <summary>
        /// 重试次数
        /// </summary>
        public int RetryCount { get; set; } = 0;

        /// <summary>
        /// 最大重试次数
        /// </summary>
        public int MaxRetries { get; set; } = 3;
    }

    /// <summary>
    /// 步骤类型枚举
    /// </summary>
    public enum StepType
    {
        /// <summary>
        /// AI生成任务
        /// </summary>
        AIGeneration,

        /// <summary>
        /// 文件操作任务
        /// </summary>
        FileOperation,

        /// <summary>
        /// 数据处理任务
        /// </summary>
        DataProcessing,

        /// <summary>
        /// 质量检查任务
        /// </summary>
        QualityCheck,

        /// <summary>
        /// 用户交互任务
        /// </summary>
        UserInteraction,

        /// <summary>
        /// 条件判断任务
        /// </summary>
        Condition,

        /// <summary>
        /// 循环任务
        /// </summary>
        Loop,

        /// <summary>
        /// 并行任务
        /// </summary>
        Parallel,

        /// <summary>
        /// 自定义任务
        /// </summary>
        Custom
    }

    /// <summary>
    /// 步骤状态枚举
    /// </summary>
    public enum StepStatus
    {
        /// <summary>
        /// 等待执行
        /// </summary>
        Pending,

        /// <summary>
        /// 执行中
        /// </summary>
        Running,

        /// <summary>
        /// 已完成
        /// </summary>
        Completed,

        /// <summary>
        /// 已失败
        /// </summary>
        Failed,

        /// <summary>
        /// 已跳过
        /// </summary>
        Skipped,

        /// <summary>
        /// 已取消
        /// </summary>
        Cancelled
    }

    /// <summary>
    /// 工作流配置
    /// </summary>
    public class WorkflowConfiguration
    {
        /// <summary>
        /// 超时时间（分钟）
        /// </summary>
        public int TimeoutMinutes { get; set; } = 60;

        /// <summary>
        /// 是否允许并行执行
        /// </summary>
        public bool AllowParallelExecution { get; set; } = false;

        /// <summary>
        /// 最大并发数
        /// </summary>
        public int MaxConcurrency { get; set; } = 1;

        /// <summary>
        /// 失败时是否自动重试
        /// </summary>
        public bool AutoRetryOnFailure { get; set; } = true;

        /// <summary>
        /// 是否在失败时继续执行
        /// </summary>
        public bool ContinueOnError { get; set; } = false;

        /// <summary>
        /// 通知配置
        /// </summary>
        public NotificationConfiguration Notifications { get; set; } = new();
    }

    /// <summary>
    /// 通知配置
    /// </summary>
    public class NotificationConfiguration
    {
        /// <summary>
        /// 是否启用通知
        /// </summary>
        public bool Enabled { get; set; } = true;

        /// <summary>
        /// 通知事件类型
        /// </summary>
        public List<NotificationEvent> Events { get; set; } = new();

        /// <summary>
        /// 通知渠道
        /// </summary>
        public List<NotificationChannel> Channels { get; set; } = new();
    }

    /// <summary>
    /// 通知事件枚举
    /// </summary>
    public enum NotificationEvent
    {
        WorkflowStarted,
        WorkflowCompleted,
        WorkflowFailed,
        StepCompleted,
        StepFailed
    }

    /// <summary>
    /// 通知渠道枚举
    /// </summary>
    public enum NotificationChannel
    {
        UI,
        Email,
        Log
    }

    /// <summary>
    /// 工作流执行结果
    /// </summary>
    public class WorkflowExecutionResult
    {
        /// <summary>
        /// 工作流ID
        /// </summary>
        public string WorkflowId { get; set; } = string.Empty;

        /// <summary>
        /// 执行状态
        /// </summary>
        public WorkflowStatus Status { get; set; }

        /// <summary>
        /// 开始时间
        /// </summary>
        public DateTime StartTime { get; set; }

        /// <summary>
        /// 结束时间
        /// </summary>
        public DateTime? EndTime { get; set; }

        /// <summary>
        /// 总执行时间
        /// </summary>
        public TimeSpan? Duration => EndTime?.Subtract(StartTime);

        /// <summary>
        /// 成功的步骤数
        /// </summary>
        public int SuccessfulSteps { get; set; }

        /// <summary>
        /// 失败的步骤数
        /// </summary>
        public int FailedSteps { get; set; }

        /// <summary>
        /// 跳过的步骤数
        /// </summary>
        public int SkippedSteps { get; set; }

        /// <summary>
        /// 总步骤数
        /// </summary>
        public int TotalSteps { get; set; }

        /// <summary>
        /// 成功率
        /// </summary>
        public double SuccessRate => TotalSteps > 0 ? (double)SuccessfulSteps / TotalSteps : 0;

        /// <summary>
        /// 执行日志
        /// </summary>
        public List<string> ExecutionLogs { get; set; } = new();

        /// <summary>
        /// 错误信息
        /// </summary>
        public List<string> Errors { get; set; } = new();

        /// <summary>
        /// 生成的文件列表
        /// </summary>
        public List<string> GeneratedFiles { get; set; } = new();

        /// <summary>
        /// 性能指标
        /// </summary>
        public Dictionary<string, object> Metrics { get; set; } = new();

        /// <summary>
        /// 输出结果
        /// </summary>
        public Dictionary<string, object> Outputs { get; set; } = new();
    }

    /// <summary>
    /// 工作流进度信息
    /// </summary>
    public class WorkflowProgress
    {
        /// <summary>
        /// 工作流ID
        /// </summary>
        public string WorkflowId { get; set; } = string.Empty;

        /// <summary>
        /// 当前步骤
        /// </summary>
        public string CurrentStep { get; set; } = string.Empty;

        /// <summary>
        /// 进度百分比 (0-100)
        /// </summary>
        public int ProgressPercentage { get; set; }

        /// <summary>
        /// 状态消息
        /// </summary>
        public string Message { get; set; } = string.Empty;

        /// <summary>
        /// 已完成步骤数
        /// </summary>
        public int CompletedSteps { get; set; }

        /// <summary>
        /// 总步骤数
        /// </summary>
        public int TotalSteps { get; set; }

        /// <summary>
        /// 预估剩余时间（分钟）
        /// </summary>
        public int? EstimatedRemainingMinutes { get; set; }

        /// <summary>
        /// 时间戳
        /// </summary>
        public DateTime Timestamp { get; set; } = DateTime.Now;
    }

    /// <summary>
    /// 工作流执行上下文
    /// </summary>
    public class WorkflowExecutionContext
    {
        /// <summary>
        /// 项目ID
        /// </summary>
        public string? ProjectId { get; set; }

        /// <summary>
        /// 用户ID
        /// </summary>
        public string? UserId { get; set; }

        /// <summary>
        /// 执行环境
        /// </summary>
        public string Environment { get; set; } = "Development";

        /// <summary>
        /// 输入参数
        /// </summary>
        public Dictionary<string, object> InputParameters { get; set; } = new();

        /// <summary>
        /// 全局变量
        /// </summary>
        public Dictionary<string, object> GlobalVariables { get; set; } = new();

        /// <summary>
        /// 配置覆盖
        /// </summary>
        public Dictionary<string, object> ConfigurationOverrides { get; set; } = new();

        /// <summary>
        /// 执行选项
        /// </summary>
        public ExecutionOptions Options { get; set; } = new();
    }

    /// <summary>
    /// 执行选项
    /// </summary>
    public class ExecutionOptions
    {
        /// <summary>
        /// 是否为调试模式
        /// </summary>
        public bool DebugMode { get; set; } = false;

        /// <summary>
        /// 是否为试运行模式
        /// </summary>
        public bool DryRun { get; set; } = false;

        /// <summary>
        /// 是否跳过用户交互
        /// </summary>
        public bool SkipUserInteraction { get; set; } = false;

        /// <summary>
        /// 是否启用详细日志
        /// </summary>
        public bool VerboseLogging { get; set; } = false;

        /// <summary>
        /// 自定义超时时间（分钟）
        /// </summary>
        public int? CustomTimeoutMinutes { get; set; }
    }

    /// <summary>
    /// 工作流状态信息
    /// </summary>
    public class WorkflowStatusInfo
    {
        /// <summary>
        /// 工作流ID
        /// </summary>
        public string Id { get; set; } = string.Empty;

        /// <summary>
        /// 工作流名称
        /// </summary>
        public string Name { get; set; } = string.Empty;

        /// <summary>
        /// 当前状态
        /// </summary>
        public WorkflowStatus Status { get; set; }

        /// <summary>
        /// 进度百分比
        /// </summary>
        public int Progress { get; set; }

        /// <summary>
        /// 当前步骤
        /// </summary>
        public string CurrentStep { get; set; } = string.Empty;

        /// <summary>
        /// 开始时间
        /// </summary>
        public DateTime StartTime { get; set; }

        /// <summary>
        /// 最后更新时间
        /// </summary>
        public DateTime LastUpdated { get; set; }
    }

    /// <summary>
    /// 工作流执行历史
    /// </summary>
    public class WorkflowExecutionHistory
    {
        /// <summary>
        /// 执行ID
        /// </summary>
        public string ExecutionId { get; set; } = Guid.NewGuid().ToString();

        /// <summary>
        /// 工作流ID
        /// </summary>
        public string WorkflowId { get; set; } = string.Empty;

        /// <summary>
        /// 开始时间
        /// </summary>
        public DateTime StartTime { get; set; }

        /// <summary>
        /// 结束时间
        /// </summary>
        public DateTime? EndTime { get; set; }

        /// <summary>
        /// 执行状态
        /// </summary>
        public Models.AgentOps.WorkflowStatus Status { get; set; }

        /// <summary>
        /// 执行结果
        /// </summary>
        public string? Result { get; set; }

        /// <summary>
        /// 错误信息
        /// </summary>
        public string? ErrorMessage { get; set; }
    }

    /// <summary>
    /// 工作流性能指标
    /// </summary>
    public class WorkflowMetrics
    {
        /// <summary>
        /// 工作流ID
        /// </summary>
        public string WorkflowId { get; set; } = string.Empty;

        /// <summary>
        /// 执行时间
        /// </summary>
        public TimeSpan ExecutionTime { get; set; }

        /// <summary>
        /// 已完成步骤数
        /// </summary>
        public int CompletedSteps { get; set; }

        /// <summary>
        /// 失败步骤数
        /// </summary>
        public int FailedSteps { get; set; }

        /// <summary>
        /// 总步骤数
        /// </summary>
        public int TotalSteps { get; set; }

        /// <summary>
        /// 平均步骤持续时间
        /// </summary>
        public TimeSpan AverageStepDuration { get; set; }

        /// <summary>
        /// 成功率
        /// </summary>
        public double SuccessRate => TotalSteps > 0 ? (double)CompletedSteps / TotalSteps : 0;
    }

    /// <summary>
    /// 任务执行结果
    /// </summary>
    public class TaskExecutionResult
    {
        /// <summary>
        /// 是否成功
        /// </summary>
        public bool Success { get; set; }

        /// <summary>
        /// 输出结果
        /// </summary>
        public string? Output { get; set; }

        /// <summary>
        /// 错误信息
        /// </summary>
        public string? ErrorMessage { get; set; }

        /// <summary>
        /// 执行时间
        /// </summary>
        public TimeSpan ExecutionTime { get; set; }

        /// <summary>
        /// 额外数据
        /// </summary>
        public Dictionary<string, object> Data { get; set; } = new();
    }

    /// <summary>
    /// 工作流错误
    /// </summary>
    public class WorkflowError
    {
        /// <summary>
        /// 错误消息
        /// </summary>
        public string Message { get; set; } = string.Empty;

        /// <summary>
        /// 异常对象
        /// </summary>
        public Exception? Exception { get; set; }

        /// <summary>
        /// 错误时间
        /// </summary>
        public DateTime Timestamp { get; set; } = DateTime.Now;

        /// <summary>
        /// 错误级别
        /// </summary>
        public ErrorLevel Level { get; set; } = ErrorLevel.Error;
    }

    /// <summary>
    /// 错误级别枚举
    /// </summary>
    public enum ErrorLevel
    {
        Warning,
        Error,
        Critical
    }

    /// <summary>
    /// 工作流优化策略
    /// </summary>
    public class WorkflowOptimizationStrategy
    {
        /// <summary>
        /// 优化目标
        /// </summary>
        public OptimizationGoal Goal { get; set; } = OptimizationGoal.Performance;

        /// <summary>
        /// 是否启用并行化
        /// </summary>
        public bool EnableParallelization { get; set; } = true;

        /// <summary>
        /// 是否优化资源使用
        /// </summary>
        public bool OptimizeResourceUsage { get; set; } = true;

        /// <summary>
        /// 是否缓存中间结果
        /// </summary>
        public bool EnableCaching { get; set; } = true;
    }

    /// <summary>
    /// 优化目标枚举
    /// </summary>
    public enum OptimizationGoal
    {
        Performance,
        Quality,
        ResourceEfficiency,
        Reliability
    }

    /// <summary>
    /// 工作流优化结果
    /// </summary>
    public class WorkflowOptimizationResult
    {
        /// <summary>
        /// 是否成功
        /// </summary>
        public bool Success { get; set; }

        /// <summary>
        /// 优化前性能
        /// </summary>
        public WorkflowMetrics BeforeOptimization { get; set; } = new();

        /// <summary>
        /// 优化后性能
        /// </summary>
        public WorkflowMetrics AfterOptimization { get; set; } = new();

        /// <summary>
        /// 改进建议
        /// </summary>
        public List<string> Improvements { get; set; } = new();

        /// <summary>
        /// 优化时间
        /// </summary>
        public DateTime OptimizationTime { get; set; } = DateTime.Now;
    }
}
